package edu.uoc.tds.server;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import edu.uoc.tds.common.Variables;

public class GestorAsistente {
	private Connection conn = null;
	
	public GestorAsistente(Connection conn) {
		this.conn = conn;
	}
	
	public Object[][] getAsistentesEvento(Integer idEvento) throws SQLException {
		Object[][] resultado = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		try {
			String sql = "SELECT u.codigo as codigo, u.nombre as nombre, " +
				"u.apellidos as apellidos, i.check_in as check_in " +
				"FROM inscripcion i, usuario u " +
				"WHERE u.codigo = i.codigo AND i.id_evento = ? " +
				"ORDER BY apellidos, nombre";
			
			ps = conn.prepareStatement(sql);
			
			ps.setInt(1, idEvento.intValue());
			rs = ps.executeQuery();
			
			List<Object[]> list = new ArrayList<Object[]>();
			Object[] fila = null;
			while (rs.next()) {
				fila = new Object[4];
				fila[0] = rs.getString("codigo");
				fila[1] = rs.getString("nombre");
				fila[2] = rs.getString("apellidos");
				if (rs.getBoolean("check_in")) {
					fila[3] = "Confirmada";
				} else {
					fila[3] = "No confirmada";					
				}
				list.add(fila);
			}
			
			if (list.size() > 0) {
				resultado = new Object[list.size()][4];
				for (int i = 0; i < list.size(); i++) {
					resultado[i] = list.get(i);
				}
			}
		} finally {
			if (ps != null) {
				ps.close();
			}
			if (rs != null) {
				rs.close();
			}
		}
		
		return resultado;
	}

}
